package com.samsung.app;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import java.util.List;

public class WifiConnector
{
  public static final int MSG_WIFI_CONNECTED = 1003;
  public static final int MSG_WIFI_DISCONNECT_THE_OTHER_CONNECTION = 1002;
  public static final int MSG_WIFI_PREPARE = 1001;
  public static final String TAG = "INRY";
  private final int SCAN_TIMEOUT = 2000;
  private final int WAIT_CONNECTING_TIMEOUT = 10000;
  private ConnectorThread mConnectorThread = null;
  private final Context mContext;
  private final Handler mHandler;
  private boolean mIsStop = false;

  public WifiConnector(Context paramContext, Handler paramHandler)
  {
    this.mContext = paramContext;
    this.mHandler = paramHandler;
  }

  private boolean sendEvent(int paramInt, String paramString)
  {
    if (this.mHandler != null)
    {
      Logger.d("INRY", "WifiConnector.sendEvent event=" + paramInt);
      Message localMessage = new Message();
      localMessage.what = paramInt;
      localMessage.obj = paramString;
      this.mHandler.sendMessage(localMessage);
    }
    return false;
  }

  public boolean start()
  {
    Logger.d("INRY", "WifiConnector.start");
    stop();
    this.mIsStop = false;
    this.mConnectorThread = new ConnectorThread();
    this.mConnectorThread.start();
    return true;
  }

  public boolean stop()
  {
    Logger.d("INRY", "WifiConnector.stop");
    this.mIsStop = true;
    if (this.mConnectorThread != null)
      this.mConnectorThread.interrupt();
    try
    {
      this.mConnectorThread.join();
      this.mConnectorThread = null;
      return true;
    }
    catch (InterruptedException localInterruptedException)
    {
      while (true)
        localInterruptedException.printStackTrace();
    }
  }

  private class ConnectedTheOtherAPException extends Exception
  {
    static final long serialVersionUID = 1304031804678079510L;

    public ConnectedTheOtherAPException(String arg2)
    {
      super();
    }
  }

  private class ConnectorThread extends Thread
  {
    WifiManager wifiManager = (WifiManager)WifiConnector.this.mContext.getSystemService("wifi");

    public ConnectorThread()
    {
      setName(getClass().getSimpleName());
    }

    private boolean connectCamAP(ScanResult paramScanResult)
    {
      boolean bool = false;
      Logger.d("INRY", "APInfo ::: " + paramScanResult.capabilities);
      WifiConfiguration localWifiConfiguration = new WifiConfiguration();
      localWifiConfiguration.SSID = "\"".concat(paramScanResult.SSID).concat("\"");
      localWifiConfiguration.status = 1;
      localWifiConfiguration.priority = 40;
      if (paramScanResult.capabilities.contains("WEP"))
      {
        Logger.d("INRY", "APInfo ::: " + paramScanResult.capabilities + "WEP");
        localWifiConfiguration.allowedKeyManagement.set(0);
        localWifiConfiguration.allowedAuthAlgorithms.set(0);
        localWifiConfiguration.allowedPairwiseCiphers.set(2);
        localWifiConfiguration.allowedGroupCiphers.set(0);
        localWifiConfiguration.wepKeys[0] = "\"".concat("qwertyui").concat("\"");
        localWifiConfiguration.wepTxKeyIndex = 0;
      }
      else if (paramScanResult.capabilities.contains("WPA"))
      {
        Logger.d("INRY", "APInfo ::: " + paramScanResult.capabilities + "WPA");
        localWifiConfiguration.allowedAuthAlgorithms.set(0);
        localWifiConfiguration.allowedProtocols.set(1);
        localWifiConfiguration.allowedKeyManagement.set(1);
        localWifiConfiguration.allowedPairwiseCiphers.set(2);
        localWifiConfiguration.allowedPairwiseCiphers.set(1);
        localWifiConfiguration.allowedGroupCiphers.set(3);
        localWifiConfiguration.allowedGroupCiphers.set(2);
        localWifiConfiguration.preSharedKey = "\"".concat("qwertyui").concat("\"");
      }
      else{
      Logger.d("INRY", "APInfo ::: " + paramScanResult.capabilities + "OPEN");
      localWifiConfiguration.allowedKeyManagement.set(0);
      localWifiConfiguration.allowedProtocols.set(1);
      localWifiConfiguration.allowedProtocols.set(0);
      localWifiConfiguration.allowedAuthAlgorithms.clear();
      localWifiConfiguration.allowedPairwiseCiphers.set(2);
      localWifiConfiguration.allowedGroupCiphers.set(0);
      localWifiConfiguration.allowedGroupCiphers.set(1);
      localWifiConfiguration.allowedGroupCiphers.set(3);
      localWifiConfiguration.allowedGroupCiphers.set(2);
      }
      int i = this.wifiManager.addNetwork(localWifiConfiguration);
      if (i != -1)
      {
        Logger.d("INRY", "netowork SSID= " + localWifiConfiguration.SSID);
        bool = this.wifiManager.enableNetwork(i, true);
        if (!bool){
      	  Logger.d("INRY", "enable Network!!!! Failed ");
      	  return false;
        }
        Logger.d("INRY", "enable Network!!!! Success");
      }
      return bool;
    }

    private String disconnectTheOtherWifi()
    {
      
      WifiInfo localWifiInfo = this.wifiManager.getConnectionInfo();
      String str = localWifiInfo.getSSID();
      if ((str != null) && (!Utils.supportDSCPrefix(str)))
      {
        this.wifiManager.disconnect();
        this.wifiManager.removeNetwork(localWifiInfo.getNetworkId());
        return str;
      }
      return null;
    }

    private boolean isConnectCamAP()
    {
      boolean bool = false;
      if (this.wifiManager.isWifiEnabled())
      {
        String str = this.wifiManager.getConnectionInfo().getSSID();
        if (str != null)
          bool = Utils.supportDSCPrefix(str);
      }
      return bool;
    }

    private boolean scanAndConnectAP()throws InterruptedException
    {
      boolean bool = false;
      long l = System.currentTimeMillis();
      while (true)
      {
        if ((bool) || (WifiConnector.this.mIsStop))
          return bool;
        this.wifiManager.startScan();
        sleep(1000L);
        List<ScanResult> localList = this.wifiManager.getScanResults();
        if (localList == null)
          continue;
        String str = disconnectTheOtherWifi();
        if (str != null)
        {
          Logger.d("INRY", "WifiConnector.disconnectTheOtherWifi ssid=" + str);
          WifiConnector.this.sendEvent(1002, str);
        }

       for(ScanResult localScanResult: localList){
          if (System.currentTimeMillis() - l <= 2000L)
            break;
          if (!Utils.supportDSCPrefix(localScanResult.SSID))
            continue;
          if (WifiConnector.this.mIsStop)
          {
        	  bool = connectCamAP(localScanResult);
        	  if (!bool)
        		  continue;
          }
        }
      }
    }

    private boolean waitForAPConnected()throws InterruptedException, WifiConnector.ConnectedTheOtherAPException
    {
      boolean bool = false;
      long l = System.currentTimeMillis();
      if ((bool) || (WifiConnector.this.mIsStop));
      while (true)
      {
     
        bool = isConnectCamAP();
        if ((bool) && (this.wifiManager.getWifiState() != 3))
          bool = false;
        sleep(200L);
        String str = disconnectTheOtherWifi();
        if (str != null)
          throw new WifiConnector.ConnectedTheOtherAPException(WifiConnector.class.getName()+">" + str + "<");
        if (System.currentTimeMillis() - l <= 10000L)
          break;
      }
      return bool;
    }

    private void waitForWifiEnabled()
      throws InterruptedException
    {
      while (true)
      {
        if ((this.wifiManager.isWifiEnabled()) || (WifiConnector.this.mIsStop))
          return;
        sleep(500L);
      }
    }

    public void run()
    {
      Logger.d("INRY", "WifiConnector.thread start");
      try
      {
        if (isConnectCamAP())
        {
          Logger.d("INRY", "WifiConnector.isConnectCamAP true");
          WifiConnector.this.sendEvent(1001, null);
          sleep(2000L);
          WifiConnector.this.sendEvent(1003, null);
          Logger.d("INRY", "WifiConnector.thread end");
          return;
        }
        Logger.d("INRY", "WifiConnector.isConnectCamAP false");
        WifiConnector.this.sendEvent(1001, null);
        int i = this.wifiManager.getWifiState();
        if ((i == 1) || (i == 0))
        {
          Logger.d("INRY", "WifiConnector.wifiEnable(true)");
          this.wifiManager.setWifiEnabled(true);
        }
        String str = disconnectTheOtherWifi();
        if (str != null)
        {
          Logger.d("INRY", "WifiConnector.disconnectTheOtherWifi ssid=" + str);
          WifiConnector.this.sendEvent(1002, str);
        }
        Logger.d("INRY", "WifiConnector.waitForWifiEnabled >");
        waitForWifiEnabled();
        Logger.d("INRY", "WifiConnector.waitForWifiEnabled <");
        boolean bool = false;
        while (true)
        {
          if ((bool) || (WifiConnector.this.mIsStop))
          {
            sleep(2000L);
            WifiConnector.this.sendEvent(1003, null);
            break;
          }
          Logger.d("INRY", "WifiConnector.scanAndConnectAP >");
          if (!scanAndConnectAP())
          {
            sleep(1000L);
            continue;
          }
          try
          {
            Logger.d("INRY", "WifiConnector.waitForAPConnected >");
            bool = waitForAPConnected();
            Logger.d("INRY", "WifiConnector.waitForAPConnected <");
            Logger.d("INRY", "WifiConnector.scanAndConnectAP <");
          }
          catch (WifiConnector.ConnectedTheOtherAPException localConnectedTheOtherAPException)
          {
            while (true)
            {
              Logger.d("INRY", "WifiConnector.ConnectedTheOtherAPException" + localConnectedTheOtherAPException);
              bool = false;
            }
          }
        }
      }
      catch (InterruptedException localInterruptedException)
      {
         Logger.d("INRY", "WifiConnector.thread end");
      		return;
      }
    }
  }
}

/* Location:           C:\Users\Chris\Desktop\viewfinder\com.samsung.app-34_dex2jar.jar
 * Qualified Name:     com.samsung.app.WifiConnector
 * JD-Core Version:    0.6.0
 */